<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

	public function User()
	{
		parent::__construct();
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->library('session');
		$this->load->model('Usermodel');
	}
	
	public function index()
	{
		$this->load->view('login_view', null);
	}
	
	public function login()
	{
		$message = '';
		if(isset($_POST['password']) && isset($_POST['username'])){
			if (substr_count($_POST['username'], " ") != 0 ||
				substr_count($_POST['password'], " ") != 0) {
				$message = "Spaces are not allowed.";
			}
			else {
				// Check login and corresponding password
				if ($this->Usermodel->loginPassValid($_POST['username'], $_POST['password'])) {
					$userId = $this->Usermodel->getUserIdByLogin($_POST['username']);
					if ($this->Usermodel->isUserConfirmed($userId)) {
						$data = array ('username' => $_POST['username']);
						$this->session->set_userdata($data);
						redirect('');
					}
					else {
						$message = 'You have not been confirmed by administrator yet.';
					}
				}
				else {
					$message = 'Incorrect login or password.';
				}				
			}
		}
		$errorData = array ('message' => $message);
		$this->load->view('login_view', $errorData);
	}
	
	public function logout() {
		$data = array ('username' => '');
		$this->session->set_userdata($data);
		redirect('');
	}
	
	public function profile()
	{
		$message = '';
		$username = $this->session->userdata('username');
        if($username){
			if (isset($_POST['name']) && isset($_POST['surname']) &&
				isset($_POST['tel_number']) && isset($_POST['email'])) {
				if (substr_count($_POST['name'], " ") != 0 ||
					substr_count($_POST['surname'], " ") != 0 ||
					substr_count($_POST['tel_number'], " ") != 0 ||
					substr_count($_POST['email'], " ") != 0) {
					$message = "Spaces are not allowed.";
				}
				else {
					$userId = $this->Usermodel->getUserIdByLogin($username);
					$this->Usermodel->setUserData($userId, $_POST['name'], $_POST['surname'],
					$_POST['email'], $_POST['tel_number']);
					if (isset($_POST['new_password']) && isset($_POST['password_confirm'])) {
						if ($_POST['new_password'] == $_POST['password_confirm'] &&
							$_POST['new_password'] != "") {
							$this->Usermodel->changePass($userId, $_POST['new_password']);
						}
					}
					$message = "Data has been changed successfully.";
				}
			}
			
			$userData = $this->Usermodel->getUserData($username);
			$data = array('username' => $userData['Login'],
						  'name' => $userData['Name'],
						  'surname' => $userData['Surname'],
						  'tel_number' => $userData['TelNumber'],
						  'email' => $userData['Email'],
						  'message' => $message);
			$this->load->view('profile_view', $data	);
        }
        else{
            $this->load->view('login_view', null);
        }
	}
	
	public function registration()
	{
		$message = '';
		if (isset($_POST['password']) && isset($_POST['username']) &&
			isset($_POST['password_confirm']) && isset($_POST['email'])){
			if (substr_count($_POST['username'], " ") != 0 ||
				substr_count($_POST['password'], " ") != 0 ||
				substr_count($_POST['password_confirm'], " ") != 0 ||
				substr_count($_POST['email'], " ") != 0) {
				$message = "Spaces are not allowed.";
			}
			else {
				if ($this->Usermodel->loginExists($_POST['username']))
				{
					$message = "User with login '".$_POST['username']."' already exists.";
				}
				else {
					if($_POST['password'] == $_POST['password_confirm'])
					{
						$this->Usermodel->addUser($_POST['username'], $_POST['password'], $_POST['email']);
						$message = "Registration completed successfully.";
					}
					else
					{
						$message = "Passwords does not match.";
					}
				}
			}
		}
		$data = array ('message' => $message);
		$this->load->view('registration_view', $data);
	}
	
	public function delete() {
		$username = $this->session->userdata('username');
        if($username){
			$userId = $this->Usermodel->getUserIdByLogin($username);
			$this->Usermodel->setDeleteRequest($userId, true);
			$data['username'] = $username;
			$this->load->view("main_view", $data);
        }
        else{
            $this->load->view('login_view', null);
        }
	}
}